package de.schweigerconsulting.util.csv;

/**
 * Copyright 2011 Stefan Schweiger
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License. 
 * 
 * The CsvRecord class provides the data of one data record from a csv file and
 * all required methods to handle it.
 * 
 * @author Stefan Schweiger
 * @version 0.1.0 (Date: 12/15/2009)
 * 
 */
public class CsvRecord {
	/**
	 * The <code>char</code> array <code>data</code> contains the several data
	 * fields.
	 */
	private String[] data;

	/**
	 * The record's headline.
	 * 
	 * @see CsvHeadline
	 */
	private CsvHeadline headline;

	/**
	 * Class constructor specifying <code>data</code>.
	 * 
	 * @param data
	 *            A <code>char</code> array containing all record data.
	 */
	protected CsvRecord(String[] data) {
		super();
		this.data = data;
	}

	/**
	 * Class constructor specifying <code>data</code> and headline.
	 * 
	 * @param data
	 *            A <code>char</code> array containing all record data.
	 * @param headline
	 *            The dedicated <code>CsvHeadline</code> object.
	 * @see CsvHeadline
	 */
	protected CsvRecord(String[] data, CsvHeadline headline) {
		super();
		this.data = data;
		this.headline = headline;
	}

	/**
	 * Returns the dedicated headline.
	 * 
	 * @return The dedicated headline.
	 * @see CsvHeadline
	 */
	protected CsvHeadline getHeadline() {
		return headline;
	}

	/**
	 * Sets the <code>headline</code> property.
	 * 
	 * @param headline
	 *            The dedicated headline.
	 * @see CsvHeadline
	 */
	protected void setHeadline(CsvHeadline headline) {
		this.headline = headline;
	}

	/**
	 * Returns the number of fields.
	 * 
	 * @return The number of fields.
	 */
	public int size() {
		return this.data.length;
	}

	/**
	 * This method determines the field data at the requested index position.
	 * 
	 * @param index
	 *            The index of requested field data.
	 * @return Field information.
	 * @throws NullPointerException
	 */
	public String getString(int index) throws NullPointerException {
		return this.data[index];
	}

	/**
	 * This method determines the field data at the requested column.
	 * 
	 * @param columnName
	 *            The name of the requested column.
	 * @return The data of the requested column.
	 * @throws NullPointerException
	 */
	public String getString(String columnName) throws NullPointerException {
		return this.data[this.headline.getColumnIndex(columnName)];
	}
}
